<?php


namespace app\controller\api;

use app\controller\api\Base;
use app\model\AfterSalesHandleModel;
use app\model\AfterSalesModel;
use app\model\CommentModel;
use app\model\ConfigModel;
use app\model\DeliveryAttachModel;
use app\model\DeliveryStepModel;
use app\model\GoodsModel;
use app\model\ReleaseScopeModel;
use app\model\SizeModel;
use app\model\VehicleModel;
use app\Request;
use app\validate\Check;
use app\validate\RecordCheck;
use think\App;
use think\facade\Db;
use app\model\DeliveryModel;
use app\model\AddressModel;

class Record extends Base
{
    public $token;
    public $version;
    public $lang;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->token = $this->request->header('xzdToken');
        $this->version = $this->request->header('version');
        $this->lang = $this->request->header('lang');
    }

    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @param RecordCheck $check
     * @notes 发货记录
     * @author: dongdong
     * @date: 2025/1/8 11:59
     */
    public function delivery_record(Request $request,DeliveryModel $deliveryModel,RecordCheck $check){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('delivery_record')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $deliveryModel->delivery_record($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @param RecordCheck $check
     * @notes 取消发布
     * @author: dongdong
     * @date: 2025/1/11 14:22
     */
    public function cancel_delivery(Request $request,DeliveryModel $deliveryModel,RecordCheck $check){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('cancel_delivery')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $deliveryModel->cancel_delivery($param);
                $this->api_result(200, '操作成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '操作失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param DeliveryModel $deliveryModel
     * @param RecordCheck $check
     * @notes 确定报价 发货
     * @author: dongdong
     * @date: 2025/1/11 15:03
     */
    public function send_delivery(Request $request,DeliveryModel $deliveryModel,RecordCheck $check){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('send_delivery')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            } else {
                $data = $deliveryModel->send_delivery($param);
                if($data==1){
                    $this->api_result(200, '操作成功', ['data' => $data]);
                }else{
                    $this->api_result(400, '操作失败', ['data' => $data]);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '操作失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param Check $check
     * @param DeliveryModel $deliveryModel
     * @notes 订单步骤
     * @author: dongdong
     * @date: 2025/1/11 17:44
     */
    public function delivery_step(Request $request,Check $check, DeliveryModel $deliveryModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('delivery_step')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $deliveryModel->delivery_step($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param Check $check
     * @param AfterSalesModel $afterSalesModel
     * @notes 提交售后反馈
     * @author: dongdong
     * @date: 2025/1/13 9:26
     */
    public function submit_after_sales(Request $request,RecordCheck $check, AfterSalesModel $afterSalesModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('submit_after_sales')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $afterSalesModel->submit_after_sales($param);
                if($data){
                    $this->api_result(200, '请求成功', ['data' => $data]);
                }else{
                    $this->api_result(200, '请求失败', []);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param ConfigModel $configModel
     * @notes 售后类型选择
     * @author: dongdong
     * @date: 2025/1/13 10:26
     */
    public function after_sales_type_select(ConfigModel $configModel){
        $data = $configModel->select_type(4);
        $this->api_result(200, '请求成功', ['data' => $data]);
    }

    /**
     * @param ConfigModel $configModel
     * @notes 处理类型选择
     * @author: dongdong
     * @date: 2025/1/13 12:09
     */
    public function handle_type_select(ConfigModel $configModel){
        $data = $configModel->select_type(5);
        $this->api_result(200, '请求成功', ['data' => $data]);
    }


    /**
     * @param Request $request
     * @param RecordCheck $check
     * @param DeliveryAttachModel $deliveryAttachModel
     * @notes 承接用户选择
     * @author: dongdong
     * @date: 2025/1/13 11:19
     */
    public function undertake_user(Request $request,RecordCheck $check, DeliveryAttachModel $deliveryAttachModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('undertake_user')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $deliveryAttachModel->undertake_user($param);
                if($data){
                    $this->api_result(200, '请求成功', ['data' => $data]);
                }else{
                    $this->api_result(200, '请求失败', []);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param RecordCheck $check
     * @param AfterSalesModel $afterSalesModel
     * @notes 处理情况与列表
     * @author: dongdong
     * @date: 2025/1/13 13:55
     */
    public function handling_situation(Request $request,RecordCheck $check,AfterSalesModel $afterSalesModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('handling_situation')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $afterSalesModel->handling_situation($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param RecordCheck $check
     * @param AfterSalesHandleModel $afterSalesHandleModel
     * @notes 提交处理情况
     * @author: dongdong
     * @date: 2025/1/13 11:48
     */
    public function submit_Handle(Request $request,RecordCheck $check,AfterSalesHandleModel $afterSalesHandleModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('submit_Handle')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $afterSalesHandleModel->submit_Handle($param);
                if($data){
                    $this->api_result(200, '请求成功', ['data' => $data]);
                }else{
                    $this->api_result(400, '请求失败', ['data' => '']);
                }
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }


    /**
     * @param Request $request
     * @param RecordCheck $check
     * @param AfterSalesModel $afterSalesModel
     * @notes 协商一致 完成售后
     * @author: dongdong
     * @date: 2025/1/13 14:49
     */
    public function complete_after_sales(Request $request,RecordCheck $check,AfterSalesModel $afterSalesModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('complete_after_sales')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $afterSalesModel->complete_after_sales($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param Request $request
     * @param RecordCheck $check
     * @param CommentModel $commentModel
     * @notes 服务评价
     * @author: dongdong
     * @date: 2025/1/13 15:40
     */
    public function evaluate(Request $request,RecordCheck $check,CommentModel $commentModel){
        try {
            $param = $request->param();
            $param['token'] = $this->token;
            $param['version'] = $this->version;
            $param['lang'] = $this->lang;
            $result = $check->scene('evaluate')->check($param);
            if (!$result) {
                $this->api_result(400, $check->getError(), []);
            }else{
                $data = $commentModel->evaluate($param);
                $this->api_result(200, '请求成功', ['data' => $data]);
            }
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', _d($e));
        }
    }

    /**
     * @param AfterSalesHandleModel $afterSalesHandleModel
     * @notes 是否已存在平台介入
     * @author: dongdong
     * @date: 2025/1/22 14:55
     */
    public function is_have_platform(AfterSalesHandleModel $afterSalesHandleModel){
        try {
            $after_sales_id = input('after_sales_id')??'';
            $data = $afterSalesHandleModel->where(['after_sales_id'=>$after_sales_id])->where("user_type='platform'")->find();
            $this->api_result(200, '请求成功', ['data' => $data]);
        } catch (\Exception $e) {
            $this->api_result(400, '请求失败!', '');
        }
    }


    /**
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 创建运踪 选择协作员工
     * @author: dongdong
     * @date: 2025/2/20 9:42
     */
    public function delivery_task_user_select(Request $request){
        $param = $request->param();
        $user_id = $param['user_id'];
        $list = Db::table("store_driver")->where(['user_id'=>$user_id])->field("register_id id,title,title_ru,phone,type")->select();
        $arr = [];
        foreach ($list as $k =>&$v){
            $v['type_arr'] = Db::table("store_config")->where(['id'=>$v['type']])->field("title,title_ru")->find();
            array_push($arr,$v);
        }
        $this->api_result(200, '请求成功', ['data' => $arr]);
    }


    /**
     * @param Request $request
     * @param Check $check
     * @throws \think\db\exception\DbException
     * @notes 创建运踪 确定协作员工
     * @author: dongdong
     * @date: 2025/2/20 9:27
     */
    public function delivery_task_divide(Request $request, Check $check)
    {
        $param = $request->param();
        $id = $delivery_id = $param['delivery_id'] ?? '';
        $task_user_id = $param['task_user_id'] ?? '';
        $param['token'] = $this->token;
        $param['version'] = $this->version;
        $param['lang'] = $this->lang;
        $result = $check->scene('delivery_task')->check($param);
        if (!$result) {
            $this->api_result(400, $check->getError(), []);
        }else{
            Db::table("store_delivery")->where(['id'=>$id])->update(['task_user_id'=>$task_user_id]);
            $delivery = Db::table("store_delivery")->where("status=2 and id=$delivery_id")->find();
            if($delivery){
                Db::query("update store_delivery set status=3 where id=$delivery_id");
                $date = date('Y-m-d H:i:s');
                Db::query("update store_delivery_attach set yuntrace_at='$date' where delivery_id=$delivery_id");

                if($delivery['parent_id']){
                    $delivery_parent = Db::table("store_delivery")->where(["id"=>$delivery['parent_id']])->field("id,task_user_id,status")->find();
                    $parent_task_user_id = $delivery_parent['task_user_id'].','.$task_user_id;
                    $arr = [];
                    $task_arr = explode(',',$parent_task_user_id);
                    foreach ($task_arr as $k =>&$v){
                        if($v){
                            if(!in_array($v,$arr)){
                                array_push($arr,$v);
                            }
                        }
                    }
                    $parent_task_user_id = implode(',',$arr);
                    Db::table("store_delivery")->where(["id"=>$delivery['parent_id']])->update(['task_user_id'=>$parent_task_user_id]);
                    if($delivery_parent['status']==2){
                        Db::table("store_delivery")->where(["id"=>$delivery['parent_id']])->update(['status'=>3]);
                    }
                }

            }
            $this->api_result(200, '请求成功', ['data' => []]);
        }
    }


    /***
     * @param Request $request
     * @param Check $check
     * @param DeliveryStepModel $deliveryStepModel
     * @notes 创建运踪步骤
     * @author: dongdong
     * @date: 2025/2/20 10:17
     */
    public function create_step(Request $request, Check $check,DeliveryStepModel $deliveryStepModel){
        $param = $request->param();
        $param['token'] = $this->token;
        $param['version'] = $this->version;
        $param['lang'] = $this->lang;


        $result = $check->scene('create_step')->check($param);
        if (!$result) {
            $this->api_result(400, $check->getError(), []);
        }else{
            $data = $deliveryStepModel->create_step($param);
            if($data=='1'){
                $this->api_result(200, '请求成功', ['data' => []]);
            }else{
                $this->api_result(400, '请求失败', ['data' => []]);
            }
        }
    }


}